﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Top.Api.Domain;
using Top.Api;
using System.Configuration;
using Top.Api.Request;
using TopModel;
using Top.Api.Response;
using TopUtility;
public partial class RateList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            hidPage.Value = "1";
            hidPageCount.Value = "20";
            ViewState["Type"] = ERateType.neutral;
            ViewState["PageCount"] = 20;
            GetData(Convert.ToInt32(hidPage.Value), Convert.ToInt32(hidPageCount.Value), (ERateType)Enum.Parse(typeof(ERateType), ViewState["Type"].ToString()));
        }
    }

    protected List<TradeRate> GetTradeRates(int pageNo, int pageCount,ERateType type)
    {
        TraderatesGetResponse rsp = new TraderatesGetResponse();
        try
        {
            ITopClient topClient = new DefaultTopClient(ConfigurationManager.AppSettings["TopInterfaceUrl"], ConfigurationManager.AppSettings["TopAppKey"], ConfigurationManager.AppSettings["TopAppSecret"], "xml");
            TraderatesGetRequest req = new TraderatesGetRequest();
            req.Fields = "tid,nick,item_title,content,result";
            req.RateType = "give";
            //req.Result = type.ToString();
            req.Role = "buyer";
            req.PageNo = pageNo;
            req.PageSize = pageCount;
            rsp = topClient.Execute(req, Session["TopSession"].ToString());
            hidTotalPage.Value = (rsp.TotalResults / pageCount + 1).ToString();
            hidTotalResults.Value = rsp.TotalResults.ToString();
        }
        catch (Exception e)
        {
            throw;
        }
        return rsp.TradeRates;
    }

    protected List<RateDetail> GetTradeDetails(List<TradeRate> rateList)
    {
        List<RateDetail> detailList = new List<RateDetail>();
        try
        {
            ITopClient topClient = new DefaultTopClient(ConfigurationManager.AppSettings["TopInterfaceUrl"], ConfigurationManager.AppSettings["TopAppKey"], ConfigurationManager.AppSettings["TopAppSecret"], "xml");
            TradeFullinfoGetRequest req = new TradeFullinfoGetRequest();
            TradeFullinfoGetResponse rsp = new TradeFullinfoGetResponse();
            RateDetail detail = null;
            Trade trade = null;
            req.Fields = "receiver_name,receiver_mobile, receiver_phone,receiver_city,receiver_state,receiver_district,receiver_address";
            foreach (TradeRate rate in rateList)
            {
                req.Tid = rate.Tid;
                rsp = topClient.Execute(req, Session["TopSession"].ToString());
                trade = rsp.Trade;
                detail = new RateDetail() { Tid = rate.Tid, Title = rate.ItemTitle, Content = rate.Content, RateType = (ERateType)Enum.Parse(typeof(ERateType), rate.Result), ReceiverMobile = trade.ReceiverMobile, ReceiverPhone = trade.ReceiverPhone, ReceiverName = trade.ReceiverName, Nick = rate.Nick, ReceiverAddress = trade.ReceiverAddress, ReceiverCity = trade.ReceiverCity, ReceiverDistrict = trade.ReceiverDistrict, ReceiverState = trade.ReceiverState };
                detailList.Add(detail);
            }
        }
        catch (Exception e)
        {
            throw;
        }
        return detailList;
    }

    protected void GetData(int pageNo, int pageCount, ERateType type)
    {
        if (pageNo > 0 && pageCount > 0)
        {
            try
            {
                List<TradeRate> rateList = GetTradeRates(pageNo, pageCount, type);
                reqRates.DataSource = GetTradeDetails(rateList);
                reqRates.DataBind();
                int page = Convert.ToInt32(hidPage.Value);
                int TotalPage = Convert.ToInt32(hidTotalPage.Value);
                if (page >= TotalPage)
                {
                    lbtnLast.Enabled = false;
                    lbtnNext.Enabled = false;
                }
                if (page <= 1)
                {
                    lbtnFirst.Enabled = false;
                    lbtnPrevious.Enabled = false;
                }
            }
            catch (Exception e)
            {
                Utility.GoError(Context, "获取评价列表错误");
            }
        }
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        try
        {
            int page = Convert.ToInt32(hidPage.Value);
            page = page + 1;
            GetData(page, Convert.ToInt32(hidPageCount.Value), (ERateType)Enum.Parse(typeof(ERateType), ViewState["Type"].ToString()));
        }
        catch (Exception ex)
        {
            Utility.GoError(Context, "获取评价列表错误");
        }
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        try
        {
            GetData(Convert.ToInt32(hidTotalPage.Value), Convert.ToInt32(hidPageCount.Value), (ERateType)Enum.Parse(typeof(ERateType), ViewState["Type"].ToString()));
        }
        catch (Exception ex)
        {
            Utility.GoError(Context, "获取评价列表错误");
        }
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        try
        {
            int page = Convert.ToInt32(hidPage.Value);
            lbtnNext.Enabled = false;
            page = page - 1;
            GetData(page, Convert.ToInt32(hidPageCount.Value), (ERateType)Enum.Parse(typeof(ERateType), ViewState["Type"].ToString()));
        }
        catch (Exception ex)
        {
            Utility.GoError(Context, "获取评价列表错误");
        }
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        try
        {
            GetData(1, Convert.ToInt32(hidPageCount.Value), (ERateType)Enum.Parse(typeof(ERateType), ViewState["Type"].ToString()));
        }
        catch (Exception ex)
        {
            Utility.GoError(Context, "获取评价列表错误");
        }
    }
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        ViewState["Type"] = ddlRType.SelectedValue;
        GetData(1, Convert.ToInt32(hidPageCount.Value), (ERateType)Enum.Parse(typeof(ERateType), ViewState["Type"].ToString()));
    }
}